package com.taotao.service.impl;

import com.taotao.entity.*;
import com.taotao.entity.item.category.EUTreeNode;
import com.taotao.mapper.ContentCategoryMapper;
import com.taotao.mapper.ContentMapper;
import com.taotao.service.ContentService;
import com.taotao.vo.ResultVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * author: 王明跃
 * Create at: 18/7/10下午4:56
 */
@Service
public class ContentServiceImpl implements ContentService {

    @Resource
    private ContentMapper contentMapper;
    @Resource
    private ContentCategoryMapper contentCategoryMapper;

    @Override
    public List<EUTreeNode> getCategoryList(Long parentId) {
        // 1. 创建查询条件
        ContentCategoryExample contentCategoryExample = new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria = contentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(parentId); // 查询父节点下的所有子节点
        // 2. 根据条件查询
        List<ContentCategory> contentCategoryList =
                contentCategoryMapper.selectByExample(contentCategoryExample);
        List<EUTreeNode> resultList = new ArrayList<>();
        // 3. 把列表转换成 EasyUI Tree 需要的json格式
        for (ContentCategory contentCategory : contentCategoryList) {
            EUTreeNode node = new EUTreeNode();
            node.setId(contentCategory.getId());
            node.setText(contentCategory.getName());
            node.setState(contentCategory.isParent() == false ? "open" : "closed");
            resultList.add(node);
        }
        // 4. 返回结果
        return resultList;
    }

    @Override
    public ContentCategory create(Long parentId, String name) {
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setParentId(parentId)
                .setName(name)
                .setParent(false)
                .setCreated(new Date())
                .setUpdated(new Date())
                .setSortOrder(1);
        // 新增的内容分类
        contentCategoryMapper.insertSelective(contentCategory);
        // 查询新增的内容分类
        contentCategory = contentCategoryMapper.selectByPrimaryKey(contentCategory.getId());
        ContentCategory category = new ContentCategory();
        // 新增的分类的parentId就是原分类的id
        category.setId(contentCategory.getParentId())
                .setParent(true);
        contentCategoryMapper.updateByPrimaryKeySelective(category);
        return contentCategory;
    }

    @Override
    public boolean update(Long id, String name) {
        ContentCategoryExample contentCategoryExample =
                new ContentCategoryExample();
        ContentCategoryExample.Criteria criteria =
                contentCategoryExample.createCriteria();
        criteria.andIdEqualTo(id);
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setName(name)
                .setUpdated(new Date());
        int update = contentCategoryMapper.updateByExampleSelective(
                contentCategory, contentCategoryExample);
        return update > 0;
    }

    @Override
    public boolean delete(Long id) {
        ContentCategory contentCategory = new ContentCategory();
        contentCategory.setId(id)
                .setStatus(0)
                .setUpdated(new Date());
        int delete = contentCategoryMapper.updateByPrimaryKeySelective(contentCategory);
        return delete > 0;
    }


    @Override
    public List<Content> queryList() {
        List<Content> contentList = contentMapper.selectByExample(null);
        return contentList;
    }

    @Override
    public ResultVO delete(Long[] ids) {
        ResultVO resultVO = new ResultVO();
        int del = contentMapper.del(ids);
        if (del > 0) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }

    @Override
    public ResultVO save(Content content) {
        ResultVO resultVO = new ResultVO();
        content.setCreated(new Date())
                .setUpdated(new Date());
        int i = contentMapper.insertSelective(content);
        if (i > 0) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }

    @Override
    public ResultVO edit(Content content) {
        ResultVO resultVO = new ResultVO();
        content.setUpdated(new Date());
        int del = contentMapper.updateByPrimaryKey(content);
        if (del > 0) {
            resultVO.setStatus(200);
        }
        return resultVO;
    }
}
